package cn.zhangxh.org.controller;

import cn.zhangxh.org.domain.Department;
import cn.zhangxh.org.query.DepartmentQuery;
import cn.zhangxh.org.service.IDepartmentService;
import cn.zhangxh.util.AjaxResult;
import cn.zhangxh.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @param :
 * @Author zxh
 * @Date: 2022/1/19$ 17:38$
 * @Version 1.0
 * @return :
 * @Description: 部门管理接口
 * 要求前后端接口对接的数据传输格式:必须是json格式(json是个对象)
 * 写接口的三要素:
 *  *  1.请求地址是什么
 *  *  2.请求的参数是什么
 *  *  3.返回值是什么
 */
@RestController
@RequestMapping("/dept")
//@CrossOrigin    //允许跨域访问,不用
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * @Date: 2022/1/24 11:37
     * @return :
     * @param :
     * @Description : 查找部门树,选择上级部门用
     */
    @PostMapping("/tree")
    @ApiOperation(value = "查找部门树",notes = "选择上级部门用")
    public List<Department> findTree(){
        return departmentService.findTree();
    }

    /**
     * @Date: 2022/1/19 22:10
     * @return :
     * @param :
     * @Description : 新增或者修改方法
     */
    @PutMapping("/change")
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    /* @RequestBody : 接收请求的json对象 */
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ok().setMessage("后端系统错误~~~");
        }
    }
    /**
     * @Date: 2022/1/19 17:59
     * @return :
     * @param :
     * @Description :删除操作
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "部门删除",notes = "传入id,通过id删除部门")
    public AjaxResult removeById(@PathVariable("id") Long id ){
        try {
            departmentService.delete(id);
            return AjaxResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            /*链式编程,所谓高级玩法*/
            return AjaxResult.ok().setMessage("后端系统错误!!!!");
        }
    }
    /**
     * @Date: 2022/1/19 17:56
     * @return :
     * @param :
     * @Description : 分页查询
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查找部门",notes = "通过分页条件,查找")
    public PageInfo<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }
    /**
     * @Date: 2022/1/19 17:44
     * @return :
     * @param :
     * @Description : 查询所有列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有部门",notes = "")
    public List<Department> findAll(){
        return departmentService.queryAll();
    }
    /**
     * @Date: 2022/1/19 17:43
     * @return :
     * @param :
     * @Description : 通过id查找一条department
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查找某个部门",notes = "传入id,通过id查找某个部门")
    public Department findById(@PathVariable("id") Long id){
        //查找一个
        return departmentService.queryById(id);
    }
    /**
     * @Date: 2022/1/22 23:11
     * @return :
     * @param :
     * @Description : 批量删除
     */
    @PatchMapping("/batch")
    @ApiOperation(value = "批量删除删除",notes = "空")
    public AjaxResult batchRemove(@RequestBody List<Long> id){
        try {
            departmentService.batchRemove(id);
            return AjaxResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            /*链式编程,所谓高级玩法*/
            return AjaxResult.ok().setMessage("后端系统错误!!!!");
        }
    }








}
